/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.ulss.monitor.mainserver;

import cn.ac.iie.commons.config.RunTimeEnv;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import cn.ac.iie.commons.config.Configuration;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;

/**
 *
 * @author YY
 */
public class ServerStartup {

    static Server server = null;
    static Logger logger = Logger.getLogger(ServerStartup.class.getName());

    public static void showUseage() {
        System.out.println("Usage:java -jar ");
    }

    private static void startup() throws Exception {
        logger.info("starting data dispatch serverl.......");
        server.start();
        logger.info("start data dispatch server successfully");
        server.join();
    }

    private static void init() throws Exception {
        String configurationFileName = "ulss-1xx-monitor.properties";
        logger.info("initializing data dispatch server...");
        logger.info("getting configuration from configuration file" + configurationFileName);
        Configuration conf = Configuration.getConfiguration(configurationFileName);
        if (conf == null) {
            throw new Exception("reading" + configurationFileName + "is failed.");
        }

        logger.info("initializing runtime enviroment...");
        if (!RunTimeEnv.initialize(conf)) {
            throw new Exception("initializing runtime enviroment is failed");
        }
        logger.info("initialize runtime enviroment successfully");
        Connector connector = new SelectChannelConnector();
        connector.setHost((String) RunTimeEnv.getParam(RunTimeEnv.JETTY_SERVER_IP));
        connector.setPort((Integer) RunTimeEnv.getParam(RunTimeEnv.JETTY_SERVER_PORT));
        server = new Server();
        server.setConnectors(new Connector[]{connector});
        server.setThreadPool(new QueuedThreadPool((Integer) RunTimeEnv.getParam(RunTimeEnv.JETTY_SERVER_THREAD_POOL_SIZE)));

        ContextHandler mqstatusContext = new ContextHandler("/loadmqstatus/json");
        LoadMqStatusHandler mqDispatcherHandler = LoadMqStatusHandler.getMqStatusHandler();
        if (mqDispatcherHandler == null) {
            throw new Exception("initializing mqDispatcherHandler is failed");
        }
        mqstatusContext.setHandler(mqDispatcherHandler);

        ContextHandler complexContext = new ContextHandler("/servicestatus/json");
        ComplexHandler infoDispatchHandler = ComplexHandler.getInfoPubHandler();
        if (infoDispatchHandler == null) {
            throw new Exception("initializing infoPubHandler is failed");
        }
        complexContext.setHandler(infoDispatchHandler);

        ContextHandler loadrateContext = new ContextHandler("/loadrate/json");
        LoadrateHandler loadrateHandler = LoadrateHandler.getInfoPubHandler();
        if (loadrateHandler == null) {
            throw new Exception("initializing loadrateHandler is failed");
        }
        loadrateContext.setHandler(loadrateHandler);

        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(new Handler[]{mqstatusContext, complexContext, loadrateContext});
        server.setHandler(contexts);

        logger.info("intialize monitor server successfully");
    }

    public static void stop() {
        try {
            server.stop();
        } catch (Exception e) {
        }
        logger.info("jetty server has been stopped");
        System.exit(0);
        logger.info("data dispatch handler has beed stopped");
    }

    public static void startMetricRefresher() {
        MetricRefresher refresher = new MetricRefresher();
        Thread t = new Thread(refresher);
        t.start();
    }

    public static void main(String[] args) {
        try {
            PropertyConfigurator.configure("log4j.properties");
            // TODO code application logic here
            init();
            startup();
        } catch (Exception ex) {
            logger.error("starting data dispatcher server is failed for " + ex.getMessage(), ex);
        }
        System.exit(0);
    }
}